<?php

namespace app\admin\controller\club;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * VIP俱乐部管理
 *
 * @icon fa fa-circle-o
 */
class Vipclub extends Backend
{
    
    /**
     * CClub模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('CClub');
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->where(['type'=>2])
                    ->order($sort, $order)
                    ->count();
                    
            $list = $this->model
                    ->field('id,owner,type,name,diamond,members')
                    ->where($where)
                    ->where(['type'=>2])
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            if(empty($list)){
                $result = array("total" => $total, "rows" => $list);
                return json($result);
            }
                    
            //处理数据
            $uid_arr = $clubid_arr = [];
            foreach ($list as $val){
                $uid_arr[] = $val['owner'];
                $clubid_arr[] = $val['id'];
            }
            $total_list = Db::connect("GameDB")->table('club_total_record')->where(array('id'=>array('in',$clubid_arr)))->select();
            $today_list = Db::connect("GameDB")->table('club_day_record')->where(array('id'=>array('in',$clubid_arr),'date'=>$this->getMilliDate(date('Y-m-d 0:0:0',time()))))->select();
            $user_list = Db::connect("GameDB")->table('user')->field('uid,nickname')->where(['uid'=>array('in',$uid_arr)])->select();//用户列表
            foreach ($user_list as $val){
                $user_arr[$val['uid']] = $val['nickname'];
            }
            foreach ($total_list as $val){
                $total_array[$val['id']] = $val;
            }
            foreach ($today_list as $val){
                $today_array[$val['id']] = $val;
            }
            foreach ($list as $key=>$val){
                $list[$key]['usernum'] = count(json_decode($val['members']));//人数
                $list[$key]['nickname'] = isset($user_arr[$val['owner']])?$user_arr[$val['owner']]:'-';//管理员昵称
                $list[$key]['totalnum'] = isset($total_array[$val['id']])?$total_array[$val['id']]['room']:0;//总开房
                $list[$key]['totaldiamond'] = isset($total_array[$val['id']])?$total_array[$val['id']]['diamond']:0;//总消耗房卡
                $list[$key]['totalscore'] = isset($total_array[$val['id']])?$total_array[$val['id']]['score']:0;//荣誉分
                $list[$key]['todayscore'] = isset($today_array[$val['id']])?$today_array[$val['id']]['score']:0;//今日荣誉分
                $list[$key]['todaynum'] = isset($today_array[$val['id']])?$today_array[$val['id']]['room']:0;//今日开房
                $list[$key]['todaydiamond'] = isset($today_array[$val['id']])?$today_array[$val['id']]['diamond']:0;//今日消耗房卡
            }
                    
            $result = array("total" => $total, "rows" => $list);
            
            return json($result);
        }
        return $this->view->fetch();
    }
    

}
